package com.gopay.webtest.controller.cbrmb;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans7026Handler;
import com.gopay.webtest.vo.RemitInfo;
import com.gopay.webtest.xml.XmlDao;

@Controller
@RequestMapping("/cbrmb")
public class CbRmbRemitQueryController extends BaseController{
	
	@Resource(name = "xmlDao")
    private XmlDao xmlDao = null;
    @Resource(name = "trans7026Handler")
    private Trans7026Handler trans7026Handler;
    
    private static Logger LOGGER = Logger.getLogger(CbrmbQueryController.class);
	
	@RequestMapping(method = RequestMethod.GET, value = "/remitQuery")
    public ModelAndView cbrmbQuery() {
        ModelAndView mv = new ModelAndView();
        // 加载xml数据到
        try {
            Map<String, String> map = this.xmlDao.readData4Map("cbrmbremitquery", "cbrmbremitqueryinfo");
            mv.addObject("cbrmbremitqueryinfo", map);
            mv.setViewName("gopay.webtest.pay.cbrmbremitquery");
        } catch (DocumentException e) {
        	LOGGER.error("查询异常",e);
        } catch (IOException e) {
        	LOGGER.error("查询异常",e);
        }
        return mv;
    }
	
	@RequestMapping("/remitQuery/submit")
    public ModelAndView cbrmbQuerySubmit(RemitInfo remitInfo) {
        ModelAndView mv = new ModelAndView();
        try {
            // 进行加签
        	trans7026Handler.sign(remitInfo.getTranCode() + "_" + remitInfo.getVersion(), "signType", "signValue",remitInfo,getRequest());
        	mv.addObject("remitInfo", remitInfo);
        	if (!remitInfo.getMerchantEncode().equals("2")) {
        		mv.setViewName("gopay.webtest.pay.cbrmbremitquerySubmit");
            } else {
            	mv.setViewName("gopay.webtest.pay.cbrmbremitquerygbkSubmit");
            }
        } catch (Exception e) {
        	LOGGER.error("加签出错",e);
        }
        return mv;
    }

}
